package cc.linker.steplink.crm.merchant.controller;


import cc.linker.steplink.crm.commons.model.PcUserInfo;
import cc.linker.steplink.crm.merchant.service.QrcodeService;
import cc.linker.steplink.crm.merchant.service.acs.MerchantInfoService;
import cc.linker.steplink.crm.merchanth5.commons.UserInfoUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.text.ParseException;
import java.util.HashMap;
import java.util.Map;

/**
 * 参数二维码
 */
@RestController
@RequestMapping("/qrcode")
public class QrcodeController {

    @Autowired
    private UserInfoUtil userInfoUtil;
    @Autowired
    private QrcodeService qrcodeService;
    @Autowired
    private MerchantInfoService merchantInfoService;


    /**
     * 获取带参二维码列表
     * @param pageIndex
     * @param pageSize
     * @param request
     * @return
     */
    @GetMapping("/getlist")
    public PageInfo getList(@RequestParam(value = "pageIndex", defaultValue = "1", required = false) Integer pageIndex,
                            @RequestParam(value = "pageSize", defaultValue = "10", required = false) Integer pageSize,
                            @RequestParam(value = "isInvalid", defaultValue = "", required = false) String isInvalid,
                            HttpServletRequest request) throws ParseException {
        PcUserInfo userinfo = userInfoUtil.getPcUserInfo(request);
        Map<String,Object> param = new HashMap<>();
        if(!StringUtils.isEmpty(isInvalid)){
            param.put("isInvalid",isInvalid);
        }
        param.put("orgId",userinfo.getOrgId());
        if(userinfo.getSaler()){
            param.put("createUserId",merchantInfoService.getCustomerIdByAdminId(userinfo.getAdminId()));
        }else{
            param.put("createUserId",userinfo.getOrgId());
        }
        PageHelper.startPage(pageIndex, pageSize);
        return new PageInfo<>(qrcodeService.getList(param));
    }


    /**
     * 保存带参二维码
     * @param request
     * @param param
     */
    @PostMapping("/saveqrcode")
    public void saveQrcode(HttpServletRequest request,@RequestBody Map<String, Object> param) throws Exception {
        PcUserInfo userinfo = userInfoUtil.getPcUserInfo(request);
        param.put("orgId",userinfo.getOrgId());
        if(userinfo.getSaler()){
            param.put("createUserId",merchantInfoService.getCustomerIdByAdminId(userinfo.getAdminId()));
        }else{
            param.put("createUserId",userinfo.getOrgId());
        }
        qrcodeService.saveQrcode(param);
    }

    /**
     * 获取详情
     * @param id
     * @param request
     * @return
     * @throws ParseException
     */
    @GetMapping("/getinfo")
    public Map<String,Object> getInfo(Integer id,HttpServletRequest request) {
        PcUserInfo userinfo = userInfoUtil.getPcUserInfo(request);
        Map<String,Object> param = new HashMap<>();
        param.put("orgId",userinfo.getOrgId());
        param.put("id",id);
        if(userinfo.getSaler()){
            param.put("createUserId",merchantInfoService.getCustomerIdByAdminId(userinfo.getAdminId()));
        }else{
            param.put("createUserId",userinfo.getOrgId());
        }
        return qrcodeService.getInfo(param);
    }

    @GetMapping("/bindpublic")
    public boolean bindPublic(HttpServletRequest request){
        PcUserInfo userinfo = userInfoUtil.getPcUserInfo(request);
        return qrcodeService.bindPublic(userinfo.getOrgId());
    }

}
